//转换为JsonObject
export function toJsonObject(obj: any) {
    if (typeof obj == 'string' && isNotBlank(obj)) {
        return JSON.parse(obj);
    }
    return obj
}

//转换为JsonString
export function toJsonString(obj: any) {
    if (typeof obj == 'object' && isNotBlank(obj)) {
        return JSON.stringify(obj);
    }
    return obj;
}

/**
 * 获取数组的中心坐标（是中心，不是重心）
 * @param paths  [{x:1,y:1}]
 * @returns {{centerY: *, centerX: *}}
 */
export function getCenterPosition(paths) {
    let minX = {x: paths[0].x, y: paths[0].y}
    let maxX = {x: paths[0].x, y: paths[0].y}
    let minY = {x: paths[0].x, y: paths[0].y}
    let maxY = {x: paths[0].x, y: paths[0].y}
    for (let i = 0; i < paths.length; i++) {
        let pX = paths[i].x
        let pY = paths[i].y

        if (pX <= minX.x) {
            minX.x = pX
            minX.y = pY
        }

        if (pX >= maxX.x) {
            maxX.x = pX
            maxX.y = pY
        }

        if (pY <= minY.y) {
            minY.x = pX
            minY.y = pY
        }
        if (pY >= maxY.y) {
            maxY.x = pX
            maxY.y = pY
        }
    }
    let centerX = (maxX.x - minX.x) / 2 + minX.x
    let centerY = (maxY.y - minY.y) / 2 + minY.y
    return {centerX: centerX, centerY: centerY}
}

//普通转换为Set数组
export function toSetArray(arrays) {
    return [...new Set(arrays)]
}

export function isBlank(obj) {
    // console.log(typeof undefined, isBlank(undefined))//undefined true
    // console.log(typeof null, isBlank(null))// object true
    // console.log(typeof {}, isBlank({}))// object false
    // console.log(typeof "", isBlank(""))// string true
    // console.log(typeof 123, isBlank(123))// number false
    // console.log(typeof [], isBlank([]))//object true
    return (obj === undefined || obj === null || obj === '' || obj.length === 0);
}

export function isNotBlank(obj) {
    return !isBlank(obj)
}

export function jsonToUrlParams(data) {
    try {
        let params = [];
        for (let i in data) {
            let key = encodeURIComponent(i);
            let value = encodeURIComponent(data[i]);
            params.push(key + '=' + value);
        }
        return params.join('&');
    } catch (err) {
        return '';
    }
}

//返回深拷贝的对象
export function copyObj(obj: any) {
    if (typeof obj == 'object') {
        return JSON.parse(JSON.stringify(obj))
    }
    return obj
}

//返回深拷贝的对象，并删除指定key
export function copyObjDelete(obj: any, key: string) {
    let cpObj = copyObj(obj)
    delete cpObj[key]
    return cpObj
}

//返回深拷贝的对象，并删除多个key
export function copyObjDeletes(obj: any, keys: Array<any>) {
    let cpObj = copyObj(obj)
    keys.filter(e => {
        delete cpObj[e]
    })
    return cpObj
}

//删除首尾字符
export function delStartEnd(text, startChar, endChar) {
    // var reg1 = /(^-*|-*$)/g;
    let reg2 = new RegExp('(^' + startChar + '*|' + endChar + '*$)', 'g');
    return text.replace(reg2, '')
}

// 横杠转驼峰
export function barToHump(text) {
    let str = text.replace(/\-(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    return delStartEnd(str, '-', '-')
}

// 驼峰转横杠
export function humpToBar(text) {
    let str = text.replace(/([A-Z])/g, "-$1").toLowerCase();
    return delStartEnd(str, '-', '-')
}

//下划线转驼峰
export function _toHump(text) {
    let str = text.replace(/\_(\w)/g, function (all, letter) {
        return letter.toUpperCase();
    });
    return delStartEnd(str, '_', '_')
}

//驼峰转下划线
export function humpTo_(text) {
    let str = text.replace(/([A-Z])/g, "_$1").toLowerCase();
    return delStartEnd(str, '_', '_')
}

//打开新窗口
export function openWindow(url = '', target = '', features = '', replace = '') {
    window.open(url)
}


//遍历象数组objs，取 key的值，返回用strKey拼接的字符串
export function listToStrByKey(objs, key, strKey) {
    if (!objs) return ''

    let str = '';
    if (typeof objs == 'string') {
        objs = JSON.parse(objs)
    }
    objs.filter(e => {
        str += (e[key] + strKey)
    })

    if (!str) return '';

    if (!strKey) return str;

    return str.substring(0, str.length - strKey.length);
}

// 替换转义字符
// 把【\"】替换为【"】
// "{\"pros\":\"https://group-test.xiao-he.cnundefined\",\"cons\":\"https://group-test.xiao-he.cnundefined\",\"license\":\"https://group-test.xiao-he.cnundefined\"}"
// {"pros":"https://group-test.xiao-he.cnundefined", "cons":"https://group-test.xiao-he.cnundefined", "license":"https://group-test.xiao-he.cnundefined"}
export function escapeJson(content) {
    if (!content) return '';
    if (typeof content == 'object') {
        content = JSON.stringify(content)
    }
    //把【\"】替换为【"】
    content = content.replace(/\\"/g, '"');
    //去掉首尾【"】
    content = content.substring(1, content.length)
    content = content.substring(0, content.length - 1)
    return content
}

//元转分
export function yuanToFen(money) {
    if (money) {
        return (money * 100).toFixed(0)
    }
    return 0;
}

//分转元两位小数
export function fenToYuan(money) {
    if (money) {
        return (money * 1 / 100).toFixed(2)
    }
    return 0;
}


